<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>防抖</title>
</head>

<body>
    <script>
        function debounce(fn, delay) {
            let timer = null //借助闭包
            return function () {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(fn, delay) // 简化写法
            }
        }
        // 然后是旧代码
        function showTop() {
            var scrollTop = document.documentElement.scrollTop;
            console.log('滚动条位置：' + scrollTop);
        }
        window.onscroll = debounce(showTop, 300)

    </script>
</body>

</html>